#include "StdAfx.h"
#include "Helpers.h"
#include "FillGroupAlgorithmSinglesUnitTests.h"
#include "Group.h"
#include "Graph.h"

// tests without input

// creates a vertex with nameArr name
Vertex* CreateVertex(bool* nameArr, int size)
{
	vector<bool> name (nameArr, nameArr+size);
	return new Vertex(name);
}

// For the use of the next 4 tests - creates a graph with n=4, w=2 and
// a group containing vertex 1100
Vertex* CreateGraph4On2AndSetGroup(Graph& graph, Group& group)
{
	VertexDirectory::Clear();
	graph = TestUtils::CreateGraph4On2(graph);
	
	bool name1[] = {true, true, false, false};
	int n=4;
	vector<bool> name_bool = Vertex::GetNameFromArray(name1,n);
	
	
	Vertex* v= VertexDirectory::GetStaticCopyVertex(name_bool);
	graph.RemoveVertex(v);
	group.InsertNewVertex(v);
	return v;
}

// create group, insert and remove vertices to it 

// test a graph with n=4,w=2
TestUtils::TestResult FillSingle4On2GroupWithoutSizeLimitation()
{
	// Scenario: creates graph over vertices with  n=4, w=2 
	// creates a group with 1100 and tries to fill it
	
	// Group = 1100
	// Graph = 0110, 0011, 1001, 0101, 1010
	Graph graph;
	Group group(0);
	Vertex* v = CreateGraph4On2AndSetGroup(graph, group);

	//try to fill the group - can only add one vertex: 0011
	// no size limitations for the group
	GreedyFillGroupAlgorithm::FillSingles(graph, group, NULL);

	//validate results:
	//check the group contains only 2 vertices
	if (TestUtils::VerifyIsTrue(group.Size() == 2,"The group doesn't contain the right number of vertices") == TestUtils::TestFailed)
	{
		return TestUtils::TestFailed;
	}
	//check the group contains the *right* 2 vertices
	if (TestUtils::VerifyIsTrue(group.IsVertexInGroup(v->Name()) == true,"The group doesn't contain initial vertex") == TestUtils::TestFailed)
	{
		return TestUtils::TestFailed;
	}

	bool name2[] = {0,0,1,1};
	int n = 4;
	v = CreateVertex(name2,n);
	
	if (TestUtils::VerifyIsTrue(group.IsVertexInGroup(v->Name()) == true,"The group doesn't contain added vertex") == TestUtils::TestFailed)
	{
			return TestUtils::TestFailed;
	}

	// check that the directory was updated correctly: All vertices in the graph appear in the graph. All vertices
	// in the group point to the right group.
	if (Algorithms::ValidateVertexDirectoryState(&graph, &group) == false)
	{
		return TestUtils::TestFailed;
	}

	return TestUtils::TestPassed;
}

// Create a graph and a group, try to fill the group, without size limitation
TestUtils::TestResult FillSingle4On2GroupWithSizeLimitation()
{
	// Scenario: creates graph over vertices with  n=4, w=2 
	// creates a group with 0011, 0110 and tries to fill it, with size limitation of 1.
	// group shouldn't change
		
	// Group = 1100
	// Graph = 0110, 0011, 1001, 0101, 1010
	Graph graph;
	Group group(0);
	Vertex* v = CreateGraph4On2AndSetGroup(graph, group);

	//try to fill the group - can only add one vertex: 0011
	// size limitations for the group: 1
	GreedyFillGroupAlgorithm::FillSingles(graph, group, NULL, 1);

	//validate results:
	//check the group contains only 1 vertx
	if (TestUtils::VerifyIsTrue(group.Size() == 1,"The group doesn't contain the right number of vertices") == TestUtils::TestFailed)
	{
		return TestUtils::TestFailed;
	}
	//check the group contains the *right* vertex
	if (TestUtils::VerifyIsTrue(group.IsVertexInGroup(v->Name()) == true,"The group doesn't contain initial vertex") == TestUtils::TestFailed)
	{
		return TestUtils::TestFailed;
	}

	// check that the directory was updated correctly: All vertices in the graph appear in the graph. All vertices
	// in the group point to the right group.
	if (Algorithms::ValidateVertexDirectoryState(&graph, &group) == false)
	{
		return TestUtils::TestFailed;
	}

	return TestUtils::TestPassed;
}
	
// Create a graph and a group, try to fill the group, without size limitation
TestUtils::TestResult FillSingle4On2EmptyGroupWithoutSizeLimitation()
{
	// Scenario: creates graph over vertices with  n=4, w=2 
	// try to fill an empty group
	
	// Graph = 1100, 0110, 0011, 1001, 0101, 1010
	Graph graph;
	Group group(0);
	VertexDirectory::Clear();
	graph = TestUtils::CreateGraph4On2(graph);
	
	//try to fill the group with no size limitation - can only add 2 vertices
	GreedyFillGroupAlgorithm::FillSingles(graph, group, NULL);
	
	//validate results:
	//check the group contains only 2 vertices
	if (TestUtils::VerifyIsTrue(group.Size() == 2,"The group doesn't contain the right number of vertices") == TestUtils::TestFailed)
	{
		return TestUtils::TestFailed;
	}
	
	//check the group is independant
	if (TestUtils::VerifyIsTrue(Algorithms::ValidateGroupIsIndependent(&group) == true,"The group isn't independent") == TestUtils::TestFailed)
	{
		return TestUtils::TestFailed;
	}	

	// check that the directory was updated correctly: All vertices in the graph appear in the graph. All vertices
	// in the group point to the right group.
	if (Algorithms::ValidateVertexDirectoryState(&graph, &group) == false)
	{
		return TestUtils::TestFailed;
	}

	return TestUtils::TestPassed;
}
// Create a graph and a group, try to fill the group, without size limitation
TestUtils::TestResult FillSingle4On2EmptyGroupWithSizeLimitation()
{
	// Scenario: creates graph over vertices with  n=4, w=2 
	// try to fill an empty group, with size limitaion of 1.
	
	// Graph = 1100, 0110, 0011, 1001, 0101, 1010
	Graph graph;
	Group group(0);
	VertexDirectory::Clear();
	graph = TestUtils::CreateGraph4On2(graph);
	
	//try to fill the group with size limitation of 1
	GreedyFillGroupAlgorithm::FillSingles(graph, group, NULL, 1);

	//validate results: check the group contains only 1 vertex
	if (TestUtils::VerifyIsTrue(group.Size() == 1,"The group doesn't contain the right number of vertices") == TestUtils::TestFailed)
	{
		return TestUtils::TestFailed;
	}
	
	// check that the directory was updated correctly: All vertices in the graph appear in the graph. All vertices
	// in the group point to the right group.
	if (Algorithms::ValidateVertexDirectoryState(&graph, &group) == false)
	{
		return TestUtils::TestFailed;
	}

	return TestUtils::TestPassed;
}



